/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mnstudent.presentationlayer;

import mnstudent.global.MyTableCellRender;
import mnstudent.global.DataTable;
import mnstudent.global.Standard;
import mnstudent.businessLayer.FacultyBLL;
import mnstudent.businessLayer.MajorBLL;
import mnstudent.datatranferobject.FacultyDTO;
import mnstudent.datatranferobject.MajorDTO;
import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author lee
 */
public class frmMajor extends javax.swing.JPanel {

    private MajorBLL m_MajorBLL;
    private FacultyBLL m_FacultyBLL;

    private ArrayList<FacultyDTO> listFacultyObj;
    FacultyDTO objAddNew;//doi tuong khoa khi chon se mo len Cua so them moi khoa
    private int currentRow = 0;
    //private boolean m_isSave = false;
    private DefaultTableModel m_Model;

    /**
     * Creates new form frmFaculty
     */
    public frmMajor() {
        initComponents();
        initControls();
        prepare();
    }

    /**
     * Init Controls
     */
    private void initControls() {
       
        jxTxtMajorID.setEditable(false);//luon luon false
        jxtabDetails.setEditable(false);
        
        //neu chua them moi thi EditTable = false
        if (!this.jxtabDetails.m_isAdd && !this.jxtabDetails.m_isUpdate) {

            jxTxtNameOfMajor.setEditable(false);
            jxCbFaculty.setEnabled(false);
            jxBtnSave.setEnabled(false);
            jxBtnCancel.setEnabled(false);

            jxBtnAdd.setEnabled(true);
            jxBtnUpdate.setEnabled(true);
            jxBtnDelete.setEnabled(true);
        } else {

            jxTxtNameOfMajor.setEditable(true);
            jxCbFaculty.setEnabled(true);
            jxBtnSave.setEnabled(true);
            jxBtnCancel.setEnabled(true);

            jxBtnAdd.setEnabled(false);
            jxBtnUpdate.setEnabled(false);
            jxBtnDelete.setEnabled(false);
        }
    }

    /**
     * Prepare
     */
        
    private void prepare() {

         //TextField tinh/quan 
        jxTxtNameOfMajor.addKeyListener(new KeyAdapter() {
                public void keyTyped(KeyEvent e) {
                    int len = jxTxtNameOfMajor.getText().length();
                    if (len > Standard.MAX_LEN) {
                            e.consume();
                    }
                }
        });
        //Set lai text cac button cua dialog
        UIManager.put("OptionPane.cancelButtonText", "Hủy");
        UIManager.put("OptionPane.noButtonText", "Không");
        UIManager.put("OptionPane.okButtonText", "Có");
        UIManager.put("OptionPane.yesButtonText", "Vâng");
        //
        this.m_MajorBLL = new MajorBLL();
        this.m_FacultyBLL = new FacultyBLL();
        bindingDataToComboBox();

        m_Model = (DefaultTableModel) jxtabDetails.getModel();
        DataTable.addRowForMajor(m_MajorBLL.getAllMajor(), m_FacultyBLL.getAllFacultyToArray(), m_Model);
       // DataTable.addRow(m_MajorBLL.getAllMajorWithNameOfFaculty(), m_Model);
        //get selected item
        jxtabDetails.setDefaultRenderer(Object.class, new MyTableCellRender());
        jxtabDetails.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent lse) {

                if (!(jxtabDetails.getSelectionModel().isSelectionEmpty())) {
                    //retrieving the selected row index
                    currentRow = jxtabDetails.getSelectedRow();
                    //if a single row is selected from the table, take each cell values into the controls
                    if (jxtabDetails.getRowSelectionAllowed() && jxtabDetails.getRowCount() > 0) {
                        if (jxtabDetails.m_isAdd || jxtabDetails.m_isUpdate) {
                            int rs = JOptionPane.showOptionDialog(null,
                                    "Vui lòng lưu thay đổi trước khi chuyển sang dòng khác!",
                                    "Cảnh báo",
                                    JOptionPane.YES_NO_OPTION,
                                    JOptionPane.QUESTION_MESSAGE,
                                    null,
                                    null,
                                    null);
                            if (rs == 0)//Yes
                            {
                                save();
                            } else {
                                cancelAction();
                            }
                        } else {
                            String strMajorID = jxtabDetails.getValueAt(currentRow, 0).toString();
                            String strNameOfMajor = jxtabDetails.getValueAt(currentRow, 1).toString();
                            jxTxtMajorID.setText(strMajorID);
                            jxTxtNameOfMajor.setText(strNameOfMajor);

                            if (!jxtabDetails.getValueAt(currentRow, 2).equals("")) {
                                FacultyDTO objFaculty = (FacultyDTO) jxtabDetails.getValueAt(currentRow, 2);
                                jxCbFaculty.setSelectedItem(objFaculty);
                            }
                        }
                    }
                }
            }
        });
        //neu co dl thi do mac dinh row dau tien
        if (jxtabDetails.getRowCount() > 0) {
            jxtabDetails.setRequestFocusEnabled(true);
            jxtabDetails.requestFocus();
            jxtabDetails.changeSelection(jxtabDetails.getRowCount() - 1, jxtabDetails.getRowCount() - 1, false, false);
        }
    }

    private void bindingDataToComboBox() {
        listFacultyObj = new ArrayList<>();
        listFacultyObj = m_FacultyBLL.getAllFacultyToArray();
        objAddNew = new FacultyDTO("KHThemMoi", "Thêm mới...");
        listFacultyObj.add(objAddNew);
        DefaultComboBoxModel dfCbModel = new DefaultComboBoxModel(listFacultyObj.toArray());
        jxCbFaculty.setModel(dfCbModel);
    }

    /**
     * Refresh Data Table
     */
    private void refreshDataInJTable() {
        m_Model.fireTableDataChanged();
        m_Model.fireTableStructureChanged();
        m_Model.getDataVector().removeAllElements();
        DataTable.addRowForMajor(m_MajorBLL.getAllMajor(), m_FacultyBLL.getAllFacultyToArray(), m_Model);
        if (jxtabDetails.getRowCount() > 0) {
            jxtabDetails.setRequestFocusEnabled(true);
            jxtabDetails.requestFocus();
            jxtabDetails.changeSelection(jxtabDetails.getRowCount() - 1, jxtabDetails.getRowCount() - 1, false, false);
        }
    }

    /**
     * Ham xoa 1 nganh dc chon
     *
     * @param rowSelected
     * @return
     */
    private boolean isDelete(int rowSelected) {
        boolean m_isDelete = false;

        String id = this.jxtabDetails.getValueAt(rowSelected, 0).toString();
        String name = this.jxtabDetails.getValueAt(rowSelected, 1).toString();
        FacultyDTO objFaculty = (FacultyDTO) this.jxtabDetails.getValueAt(rowSelected, 2);
        MajorDTO majorObj = new MajorDTO(id, name, objFaculty.getFacultyID());
        if (m_MajorBLL.deleteMajorFromDatabase(majorObj)) {
            m_Model.removeRow(rowSelected);
            m_isDelete = true;
        }

        return m_isDelete;
    }

    /**
     * Ham xoa 1 luc nhieu nganh
     *
     * @return
     */
    private boolean isDeleteMoreMajor() {
        boolean m_isDelete = true;
        int[] arrRowSelected = this.jxtabDetails.getSelectedRows();
        for (int i = arrRowSelected.length - 1; i >= 0; i--) {
            if (!isDelete(arrRowSelected[i])) {
                JOptionPane.showMessageDialog(null,
                        "Xóa thất bại khoa: " + jxtabDetails.getValueAt(arrRowSelected[i], 1),
                        "Lỗi",
                        JOptionPane.YES_OPTION);
                m_isDelete = false;
            }
        }
        return m_isDelete;
    }

    /**
     * Ham thuc hien viec cap nhat va update
     */
    private void save() {
        if (this.jxtabDetails.m_isAdd) {
            saveToDatabase();
            
        } else if (this.jxtabDetails.m_isUpdate) {
            updateToDatabase();
        }
    }

    /**
     * Ham thuc hien insert Khoa xuong database
     *
     * @return
     */
    private void saveToDatabase() {
        
        if (checkData()) {
            String id = "NG0000***";
            String name = Standard.standardNamePerson(this.jxTxtNameOfMajor.getText());
            FacultyDTO objFaculty = (FacultyDTO) this.jxCbFaculty.getSelectedItem();
            String idFaculty = objFaculty.getFacultyID();
            MajorDTO majorObj = new MajorDTO(id, name, idFaculty);
            if (m_MajorBLL.insertMajorToDatabase(majorObj)) {
                 JOptionPane.showOptionDialog(null,
                        "Lưu xuống thành công!",
                        "Thông báo",
                        JOptionPane.YES_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        null,
                        null,
                        null);
                //neu luu thanh cong
                this.jxtabDetails.m_isAdd = false;
                initControls();
                refreshDataInJTable();
            } else {
                JOptionPane.showMessageDialog(null, "Lưu xuống thất bại. Vui lòng kiểm tra lại!", "Lỗi", JOptionPane.YES_OPTION);
            }
        }
    }

    /**
     * Ham thuc hien cap nhat Khoa
     *
     * @return
     */
    private void updateToDatabase() {
        if (checkData()) {
            String id = this.jxTxtMajorID.getText().trim();
            String name = Standard.standardName(this.jxTxtNameOfMajor.getText());
            FacultyDTO objFaculty = (FacultyDTO) this.jxCbFaculty.getSelectedItem();
            String idFaculty = objFaculty.getFacultyID();
            MajorDTO majorObj = new MajorDTO(id, name, idFaculty);
            if (m_MajorBLL.updateMajorToDatabase(majorObj)) {
                JOptionPane.showOptionDialog(null,
                        "Câp nhật thành công!",
                        "Thông báo",
                        JOptionPane.YES_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        null,
                        null,
                        null);
                //neu Cập nhật thanh cong
                this.jxtabDetails.m_isUpdate = false;
                initControls();
                refreshDataInJTable();
            } else {
                JOptionPane.showMessageDialog(null, "Cập nhật thất bại. Vui lòng kiểm tra lại!", "Lỗi", JOptionPane.YES_OPTION);
            }
        }
    }

    private void cancelAction() {
        if (this.jxtabDetails.m_isAdd) {
            ((DefaultTableModel) jxtabDetails.getModel()).removeRow(jxtabDetails.getRowCount() - 1);
            this.jxtabDetails.m_isAdd = false;
            initControls();
            jxTxtMajorID.setText("");
            jxTxtNameOfMajor.setText("");
            jxtabDetails.changeSelection(jxtabDetails.getRowCount() - 1, jxtabDetails.getRowCount() - 1, false, false);

        } else {
            
            this.jxtabDetails.m_isUpdate = false;
            initControls();
        }
        refreshDataInJTable();
    }

    private boolean checkData()
    {
        if(this.jxTxtNameOfMajor.getText().equals("") || this.jxCbFaculty.getSelectedIndex() == -1){
            JOptionPane.showOptionDialog(null,
                    "Dữ liệu không được bỏ trống. Vui lòng nhập lại!", 
                    "Cảnh báo",
                    JOptionPane.YES_OPTION,
                    JOptionPane.WARNING_MESSAGE,
                    null,
                    null,
                    null);
            return false;
        }
        else
        {
            if(!m_MajorBLL.checkInputIsInvalid(this.jxtabDetails, this.jxTxtNameOfMajor.getText()))
            {
                JOptionPane.showMessageDialog(null, "Ngành vừa nhập đã tồn tại trong cơ sở dữ liệu!");
                return false;
            }
        }
        return true;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jxpanDetails = new org.jdesktop.swingx.JXPanel();
        jpanTableDetails = new javax.swing.JPanel();
        jpanContent = new javax.swing.JPanel();
        jxpanFindNavibar = new org.jdesktop.swingx.JXPanel();
        jpanChose = new javax.swing.JPanel();
        jChkSearchWithMajorID = new javax.swing.JCheckBox();
        jChkSearchWithMajorName = new javax.swing.JCheckBox();
        jChkSearchFast = new javax.swing.JCheckBox();
        jChkSearchWithFacultyName = new javax.swing.JCheckBox();
        jxbtnFindVisible = new org.jdesktop.swingx.JXButton();
        jxSearchFaculty = new org.jdesktop.swingx.JXSearchField();
        jXLabel1 = new org.jdesktop.swingx.JXLabel();
        jScrollPane = new javax.swing.JScrollPane();
        jxtabDetails = new mnstudent.global.MyTable();
        jpanFuntionalDetails = new javax.swing.JPanel();
        jxBtnAdd = new org.jdesktop.swingx.JXButton();
        jxBtnUpdate = new org.jdesktop.swingx.JXButton();
        jxBtnDelete = new org.jdesktop.swingx.JXButton();
        jxBtnSave = new org.jdesktop.swingx.JXButton();
        jxBtnCancel = new org.jdesktop.swingx.JXButton();
        jxpanHeader = new org.jdesktop.swingx.JXPanel();
        jxpanTitleHeader = new org.jdesktop.swingx.JXPanel();
        jXLabel5 = new org.jdesktop.swingx.JXLabel();
        jxpanInfoHeader = new org.jdesktop.swingx.JXPanel();
        jxpanInfo = new org.jdesktop.swingx.JXPanel();
        jxLabFacultyID = new org.jdesktop.swingx.JXLabel();
        jxTxtMajorID = new org.jdesktop.swingx.JXTextField();
        jxLabNameOfFaculty = new org.jdesktop.swingx.JXLabel();
        jxTxtNameOfMajor = new org.jdesktop.swingx.JXTextField();
        jxLabNameOfFaculty1 = new org.jdesktop.swingx.JXLabel();
        jxCbFaculty = new org.jdesktop.swingx.JXComboBox();

        setLayout(new java.awt.BorderLayout());

        jxpanDetails.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jxpanDetails.setLayout(new java.awt.BorderLayout());

        jpanTableDetails.setLayout(new java.awt.BorderLayout());

        jpanContent.setLayout(new java.awt.BorderLayout());

        jxpanFindNavibar.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jxpanFindNavibar.setPreferredSize(new java.awt.Dimension(914, 30));

        jpanChose.setBorder(javax.swing.BorderFactory.createTitledBorder("Chọn nội dung tìm kiếm"));

        jChkSearchWithMajorID.setText("Tìm theo mã ngành");

        jChkSearchWithMajorName.setText("Tìm theo tên ngành");

        jChkSearchFast.setText("Tìm kiếm nhanh");

        jChkSearchWithFacultyName.setText("Tìm theo tên khoa");

        javax.swing.GroupLayout jpanChoseLayout = new javax.swing.GroupLayout(jpanChose);
        jpanChose.setLayout(jpanChoseLayout);
        jpanChoseLayout.setHorizontalGroup(
            jpanChoseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanChoseLayout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addComponent(jChkSearchWithMajorID)
                .addGap(30, 30, 30)
                .addComponent(jChkSearchWithMajorName)
                .addGap(30, 30, 30)
                .addComponent(jChkSearchWithFacultyName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 270, Short.MAX_VALUE)
                .addComponent(jChkSearchFast)
                .addGap(108, 108, 108))
        );
        jpanChoseLayout.setVerticalGroup(
            jpanChoseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanChoseLayout.createSequentialGroup()
                .addGroup(jpanChoseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jChkSearchWithMajorID)
                    .addComponent(jChkSearchFast)
                    .addComponent(jChkSearchWithFacultyName)
                    .addComponent(jChkSearchWithMajorName))
                .addGap(0, 0, 0))
        );

        jxbtnFindVisible.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jxbtnFindVisible.setText("Tùy chọn");
        jxbtnFindVisible.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxbtnFindVisibleActionPerformed(evt);
            }
        });

        jXLabel1.setText("Tìm kiếm:");

        javax.swing.GroupLayout jxpanFindNavibarLayout = new javax.swing.GroupLayout(jxpanFindNavibar);
        jxpanFindNavibar.setLayout(jxpanFindNavibarLayout);
        jxpanFindNavibarLayout.setHorizontalGroup(
            jxpanFindNavibarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jxpanFindNavibarLayout.createSequentialGroup()
                .addComponent(jxbtnFindVisible, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jxSearchFaculty, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jpanChose, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jxpanFindNavibarLayout.setVerticalGroup(
            jxpanFindNavibarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jxpanFindNavibarLayout.createSequentialGroup()
                .addGroup(jxpanFindNavibarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jxbtnFindVisible, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jxSearchFaculty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpanChose, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jpanContent.add(jxpanFindNavibar, java.awt.BorderLayout.NORTH);

        jxtabDetails.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã ngành", "Tên ngành", "Khoa"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane.setViewportView(jxtabDetails);

        jpanContent.add(jScrollPane, java.awt.BorderLayout.CENTER);

        jpanTableDetails.add(jpanContent, java.awt.BorderLayout.CENTER);

        jpanFuntionalDetails.setLayout(new java.awt.GridBagLayout());

        jxBtnAdd.setText("Thêm mới");
        jxBtnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxBtnAddActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 6, 9);
        jpanFuntionalDetails.add(jxBtnAdd, gridBagConstraints);

        jxBtnUpdate.setText("Sửa đổi");
        jxBtnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxBtnUpdateActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 6, 8);
        jpanFuntionalDetails.add(jxBtnUpdate, gridBagConstraints);

        jxBtnDelete.setText("Xóa bỏ");
        jxBtnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxBtnDeleteActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 4, 8);
        jpanFuntionalDetails.add(jxBtnDelete, gridBagConstraints);

        jxBtnSave.setText("Lưu xuống");
        jxBtnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxBtnSaveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(11, 9, 7, 8);
        jpanFuntionalDetails.add(jxBtnSave, gridBagConstraints);

        jxBtnCancel.setText("Hủy bỏ");
        jxBtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxBtnCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(12, 9, 8, 9);
        jpanFuntionalDetails.add(jxBtnCancel, gridBagConstraints);

        jpanTableDetails.add(jpanFuntionalDetails, java.awt.BorderLayout.EAST);

        jxpanDetails.add(jpanTableDetails, java.awt.BorderLayout.CENTER);

        add(jxpanDetails, java.awt.BorderLayout.CENTER);

        jxpanHeader.setLayout(new java.awt.BorderLayout());

        jxpanTitleHeader.setBorder(new org.jdesktop.swingx.border.DropShadowBorder());

        jXLabel5.setForeground(new java.awt.Color(0, 102, 255));
        jXLabel5.setText("NGÀNH");
        jXLabel5.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        jxpanTitleHeader.add(jXLabel5);

        jxpanHeader.add(jxpanTitleHeader, java.awt.BorderLayout.PAGE_START);

        jxpanInfoHeader.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jxpanInfoHeader.setLayout(new java.awt.BorderLayout());

        jxpanInfo.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "THÔNG TIN", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP));

        jxLabFacultyID.setText("Mã ngành");

        jxTxtMajorID.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jxTxtMajorIDCaretUpdate(evt);
            }
        });

        jxLabNameOfFaculty.setText("Tên ngành");

        jxTxtNameOfMajor.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jxTxtNameOfMajorCaretUpdate(evt);
            }
        });
        jxTxtNameOfMajor.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jxTxtNameOfMajorKeyPressed(evt);
            }
        });

        jxLabNameOfFaculty1.setText("Khoa");

        jxCbFaculty.setVerifyInputWhenFocusTarget(false);
        jxCbFaculty.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jxCbFacultyItemStateChanged(evt);
            }
        });
        jxCbFaculty.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jxCbFacultyKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jxpanInfoLayout = new javax.swing.GroupLayout(jxpanInfo);
        jxpanInfo.setLayout(jxpanInfoLayout);
        jxpanInfoLayout.setHorizontalGroup(
            jxpanInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jxpanInfoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jxLabFacultyID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jxTxtMajorID, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(62, 62, 62)
                .addComponent(jxLabNameOfFaculty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jxTxtNameOfMajor, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(62, 62, 62)
                .addComponent(jxLabNameOfFaculty1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jxCbFaculty, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(60, Short.MAX_VALUE))
        );
        jxpanInfoLayout.setVerticalGroup(
            jxpanInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jxpanInfoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jxpanInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jxLabFacultyID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jxTxtMajorID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jxLabNameOfFaculty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jxTxtNameOfMajor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jxLabNameOfFaculty1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jxCbFaculty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jxpanInfoHeader.add(jxpanInfo, java.awt.BorderLayout.CENTER);

        jxpanHeader.add(jxpanInfoHeader, java.awt.BorderLayout.CENTER);

        add(jxpanHeader, java.awt.BorderLayout.NORTH);
    }// </editor-fold>//GEN-END:initComponents

    private void jxbtnFindVisibleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxbtnFindVisibleActionPerformed
        if (jxbtnFindVisible.getText().equals("Đóng")) {
            jxbtnFindVisible.setPreferredSize(new Dimension(110, 25));
            jxbtnFindVisible.setText("Tùy chọn");
            jxbtnFindVisible.revalidate();
            jxpanFindNavibar.setPreferredSize(new Dimension(30, 30));
            jxpanFindNavibar.revalidate();
        } else {
            jxbtnFindVisible.setPreferredSize(new Dimension(20, 25));
            jxbtnFindVisible.setText("Đóng");
            jxbtnFindVisible.revalidate();
            jxpanFindNavibar.setPreferredSize(new Dimension(30, 105));
            jxpanFindNavibar.revalidate();
        }
    }//GEN-LAST:event_jxbtnFindVisibleActionPerformed

    private void jxBtnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxBtnAddActionPerformed
        for (int i = 0; i < jxtabDetails.getColumnCount(); i++) {
            if (jxtabDetails.getValueAt(jxtabDetails.getRowCount() - 1, i).equals("")) {
                return;
            }
        }

        Vector newRow = new Vector(jxtabDetails.getColumnCount());
        for (int i = 0; i < jxtabDetails.getColumnCount(); i++) {
            newRow.addElement("");
        }
        ((DefaultTableModel) jxtabDetails.getModel()).addRow(newRow);
        jxtabDetails.setRowSelectionInterval(jxtabDetails.getRowCount() - 1, jxtabDetails.getRowCount() - 1);
        //jxtabDetails.setEnabled(false);
        //jxtabDetails.setRowSelectionAllowed(false);
        JScrollBar sb = jScrollPane.getVerticalScrollBar();
        sb.setValue(sb.getMaximum());

        if (!this.jxtabDetails.m_isAdd) {
            this.jxtabDetails.m_isAdd = true;
            initControls();
            this.jxtabDetails.m_rowIndex = jxtabDetails.getRowCount() - 1;
            jxTxtMajorID.setText("NG0000***");
            jxCbFaculty.setSelectedItem(null);
            jxTxtNameOfMajor.requestFocus();
        }
    }//GEN-LAST:event_jxBtnAddActionPerformed

    private void jxTxtMajorIDCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jxTxtMajorIDCaretUpdate
        if (this.jxtabDetails.m_isAdd)
            jxtabDetails.getModel().setValueAt(jxTxtMajorID.getText(), jxtabDetails.getRowCount() - 1, 0);
        else 
            if (this.jxtabDetails.m_isUpdate)
                jxtabDetails.getModel().setValueAt(jxTxtMajorID.getText(), jxtabDetails.getSelectedRow(), 0);

    }//GEN-LAST:event_jxTxtMajorIDCaretUpdate

    private void jxTxtNameOfMajorCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jxTxtNameOfMajorCaretUpdate

        if(this.jxtabDetails.m_isAdd)
            jxtabDetails.getModel().setValueAt(jxTxtNameOfMajor.getText(), jxtabDetails.getRowCount()-1, 1);
        else
            if(this.jxtabDetails.m_isUpdate)
                jxtabDetails.getModel().setValueAt(jxTxtNameOfMajor.getText(), jxtabDetails.getSelectedRow(), 1);
    }//GEN-LAST:event_jxTxtNameOfMajorCaretUpdate

    private void jxBtnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxBtnDeleteActionPerformed
        int rs = JOptionPane.showOptionDialog(null,
                "Bạn có muốn xóa không!",
                "Câu hỏi",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE,
                null,
                null,
                null);
        if (rs == 0) {
            if (isDeleteMoreMajor()) {
                JOptionPane.showOptionDialog(null,
                        "Xóa thành công!",
                        "Thông báo",
                        JOptionPane.YES_OPTION,
                        JOptionPane.INFORMATION_MESSAGE,
                        null,
                        null,
                        null);
                //neu luu thanh cong
                this.jxtabDetails.m_isAdd = false;
                initControls();
                refreshDataInJTable();
            } else {
                JOptionPane.showMessageDialog(null, "Xóa thất bại. Vui lòng kiểm tra lại!", "Lỗi", JOptionPane.YES_OPTION);
            }
        }
    }//GEN-LAST:event_jxBtnDeleteActionPerformed

    private void jxBtnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxBtnSaveActionPerformed
        save();
    }//GEN-LAST:event_jxBtnSaveActionPerformed

    private void jxBtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxBtnCancelActionPerformed

        cancelAction();
    }//GEN-LAST:event_jxBtnCancelActionPerformed

    private void jxCbFacultyItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jxCbFacultyItemStateChanged
        
        if(jxCbFaculty.getSelectedItem() != null ){
            FacultyDTO obj = (FacultyDTO) evt.getItem();
            if (obj.equals(objAddNew)) {
                JOptionPane.showMessageDialog(null, "Thêm mới Khoa!");
                  jxCbFaculty.setSelectedItem(null);
            } else {
                if (this.jxtabDetails.m_isAdd || this.jxtabDetails.m_isUpdate) {
                    jxtabDetails.getModel().setValueAt(obj, currentRow, 2);
                }
            }
        }
    }//GEN-LAST:event_jxCbFacultyItemStateChanged

    private void jxBtnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxBtnUpdateActionPerformed
        jxBtnAdd.setEnabled(false);
        jxBtnDelete.setEnabled(false);
        jxBtnUpdate.setEnabled(false);

        jxBtnSave.setEnabled(true);
        jxBtnCancel.setEnabled(true);
        jxTxtNameOfMajor.setEditable(true);
        jxCbFaculty.setEnabled(true);
        jxTxtNameOfMajor.requestFocus();

        this.jxtabDetails.m_isUpdate = true;
    }//GEN-LAST:event_jxBtnUpdateActionPerformed

    private void jxCbFacultyKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jxCbFacultyKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            save();
        }
    }//GEN-LAST:event_jxCbFacultyKeyPressed

    private void jxTxtNameOfMajorKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jxTxtNameOfMajorKeyPressed

      if(jxTxtNameOfMajor.getText().length()>Standard.MAX_LEN)
            jxTxtNameOfMajor.setText(jxTxtNameOfMajor.getText().substring(0, Standard.MAX_LEN));
    }//GEN-LAST:event_jxTxtNameOfMajorKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jChkSearchFast;
    private javax.swing.JCheckBox jChkSearchWithFacultyName;
    private javax.swing.JCheckBox jChkSearchWithMajorID;
    private javax.swing.JCheckBox jChkSearchWithMajorName;
    private javax.swing.JScrollPane jScrollPane;
    private org.jdesktop.swingx.JXLabel jXLabel1;
    private org.jdesktop.swingx.JXLabel jXLabel5;
    private javax.swing.JPanel jpanChose;
    private javax.swing.JPanel jpanContent;
    private javax.swing.JPanel jpanFuntionalDetails;
    private javax.swing.JPanel jpanTableDetails;
    private org.jdesktop.swingx.JXButton jxBtnAdd;
    private org.jdesktop.swingx.JXButton jxBtnCancel;
    private org.jdesktop.swingx.JXButton jxBtnDelete;
    private org.jdesktop.swingx.JXButton jxBtnSave;
    private org.jdesktop.swingx.JXButton jxBtnUpdate;
    private org.jdesktop.swingx.JXComboBox jxCbFaculty;
    private org.jdesktop.swingx.JXLabel jxLabFacultyID;
    private org.jdesktop.swingx.JXLabel jxLabNameOfFaculty;
    private org.jdesktop.swingx.JXLabel jxLabNameOfFaculty1;
    private org.jdesktop.swingx.JXSearchField jxSearchFaculty;
    private org.jdesktop.swingx.JXTextField jxTxtMajorID;
    private org.jdesktop.swingx.JXTextField jxTxtNameOfMajor;
    private org.jdesktop.swingx.JXButton jxbtnFindVisible;
    private org.jdesktop.swingx.JXPanel jxpanDetails;
    private org.jdesktop.swingx.JXPanel jxpanFindNavibar;
    private org.jdesktop.swingx.JXPanel jxpanHeader;
    private org.jdesktop.swingx.JXPanel jxpanInfo;
    private org.jdesktop.swingx.JXPanel jxpanInfoHeader;
    private org.jdesktop.swingx.JXPanel jxpanTitleHeader;
    private mnstudent.global.MyTable jxtabDetails;
    // End of variables declaration//GEN-END:variables
}
